﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KBVault.DAL.Entities;
using Codebricks.Db;
using Codebricks.Db.Extensions;
using System.Data;

namespace KBVault.DAL.Repository
{
    public class AttachmentRepository:BaseRepository
    {
        private static string GetArticleAttachmentsCommand = "Select * from article_attachments where article_id = @ArticleId";
        private static string UpdateAttachmentArticleByHashKey = "update article_attachments set article_id=@ArticleId where hash=@HashKey";        

        public static void Create( ref Attachment a)
        {
            try
            {
                a.Id = MySqlDb.Insert(a);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
        }

        public static void Update(Attachment a)
        {
            try
            {
                MySqlDb.Update(a);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
        }

        public static void Delete(int id)
        {
            try
            {
                Attachment a = new Attachment() { Id = id };                
                MySqlDb.Delete(a);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
        }

        public static Attachment Get(int id)
        {
            try
            {
                Attachment a = new Attachment() { Id = id };
                a = MySqlDb.Get<Attachment>(a);                
                return a;
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
        }

        public static void UpdateArticleIdByHash(long articleId,string hash)
        {
            try
            {
                MysqlParameterCollection col = new MysqlParameterCollection();
                col.Add("ArticleId", articleId);
                col.Add("HashKey", hash);
                MySqlDb.ExecuteQuery(UpdateAttachmentArticleByHashKey, col);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
        }
        public static List<Attachment> GetArticleAttachments( int articleId )
        {
            try
            {
                List<Attachment> attachments = new List<Attachment>();
                MysqlParameterCollection col = new MysqlParameterCollection();
                col.Add("ArticleId", articleId);
                DataTable dt = MySqlDb.Select(GetArticleAttachmentsCommand, col);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        Attachment a = new Attachment();
                        a.CreateObjectFromDataRow(row);
                        attachments.Add(a);
                    }
                }
                return attachments;
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
        }
    }
}
